

package com.zdkj.subway.support.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.annotation.Inner;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.support.entity.SwPmTicketCardType;
import com.zdkj.subway.support.service.SwPmTicketCardTypeService;
import com.zdkj.subway.support.util.UploadDataListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * 主类型票卡参数表
 *
 * @author cuilei
 * @date 2020-06-01 14:16:07
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swpmticketcardtype")
@Api(value = "swpmticketcardtype", tags = "主类型票卡参数表管理(票种)")
public class SwPmTicketCardTypeController {

    private final SwPmTicketCardTypeService swPmTicketCardTypeService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param swPmTicketCardType 主类型票卡参数表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwPmTicketCardTypePage(Page page, SwPmTicketCardType swPmTicketCardType) {
        return R.ok(swPmTicketCardTypeService.page(page, Wrappers.query(swPmTicketCardType)));
    }

    /**
     * 通过id查询主类型票卡参数表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swPmTicketCardTypeService.getById(id));
    }

    /**
     * 新增主类型票卡参数表
     *
     * @param swPmTicketCardType 主类型票卡参数表
     * @return R
     */
    @ApiOperation(value = "新增主类型票卡参数表", notes = "新增主类型票卡参数表")
    @SysLog("新增主类型票卡参数表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_ticket_card_type_add')")
    public R save(@RequestBody SwPmTicketCardType swPmTicketCardType) {
        SwPmTicketCardType defaultObj = ObjectUtils.getDefaultObj(SwPmTicketCardType.class);
        BeanUtils.copyProperties(swPmTicketCardType, defaultObj,
                ObjectUtils.getNullAndEmptyPropertyNames(swPmTicketCardType));
		
		QueryWrapper queryWrapper=new QueryWrapper();       
        queryWrapper.eq("main_name",swPmTicketCardType.getMainName());       
        queryWrapper.eq("del_flag","0");
        int mainNameCount=swPmTicketCardTypeService.count(queryWrapper);
        if(mainNameCount>0){
            return R.failed(swPmTicketCardType.getMainName()+" 数据已存在！");
        }
		QueryWrapper queryWrapperEn=new QueryWrapper();       
        queryWrapperEn.eq("main_en_name",swPmTicketCardType.getMainEnName());       
        queryWrapperEn.eq("del_flag","0");
        int mainEnNameCount=swPmTicketCardTypeService.count(queryWrapperEn);
        if(mainEnNameCount>0){
            return R.failed(swPmTicketCardType.getMainEnName()+" 数据已存在！");
        }
		
        return R.ok(swPmTicketCardTypeService.save(defaultObj));
    }

    /**
     * 修改主类型票卡参数表
     *
     * @param swPmTicketCardType 主类型票卡参数表
     * @return R
     */
    @ApiOperation(value = "修改主类型票卡参数表", notes = "修改主类型票卡参数表")
    @SysLog("修改主类型票卡参数表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_ticket_card_type_edit')")
    public R updateById(@RequestBody SwPmTicketCardType swPmTicketCardType) {

		QueryWrapper queryWrapper=new QueryWrapper();       
        queryWrapper.eq("main_name",swPmTicketCardType.getMainName());       
        queryWrapper.eq("del_flag","0");
		queryWrapper.ne("id",swPmTicketCardType.getId());
        int mainNameCount=swPmTicketCardTypeService.count(queryWrapper);
        if(mainNameCount>0){
            return R.failed(swPmTicketCardType.getMainName()+" 数据已存在！");
        }
		QueryWrapper queryWrapperEn=new QueryWrapper();       
        queryWrapperEn.eq("main_en_name",swPmTicketCardType.getMainEnName());       
        queryWrapperEn.eq("del_flag","0");
		queryWrapperEn.ne("id",swPmTicketCardType.getId());
        int mainEnNameCount=swPmTicketCardTypeService.count(queryWrapperEn);
        if(mainEnNameCount>0){
            return R.failed(swPmTicketCardType.getMainEnName()+" 数据已存在！");
        }

        return R.ok(swPmTicketCardTypeService.updateById(swPmTicketCardType));
    }

    /**
     * 通过id删除主类型票卡参数表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除主类型票卡参数表", notes = "通过id删除主类型票卡参数表")
    @SysLog("通过id删除主类型票卡参数表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_pm_ticket_card_type_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swPmTicketCardTypeService.removeById(id));
    }

    /**
     * 主类型票卡参数字典
     *
     * @return
     */
    @ApiOperation(value = "主类型票卡参数字典", notes = "主类型票卡参数字典")
    //@SysLog("主类型票卡参数字典")
    @GetMapping("/dict")
    public R getDict() {
        return R.ok(swPmTicketCardTypeService.getBaseMapper().selectList(
                Wrappers.<SwPmTicketCardType>lambdaQuery().eq(SwPmTicketCardType::getStatus,"1")));
    }

    /**
     * 主类型票卡参数单程票
     *
     * @return
     */
    @ApiOperation(value = "主类型票卡参数单程票", notes = "主类型票卡参数单程票")
    //@SysLog("主类型票卡参数单程票")
    @GetMapping("/dict/DCP")
    public R getDictDCP() {
        //QueryWrapper wrapper = Wrappers.query();
        SwPmTicketCardType swPmTicketCardType = new SwPmTicketCardType();
        LambdaQueryWrapper<SwPmTicketCardType> queryWrappers = Wrappers.query(swPmTicketCardType).lambda();
        queryWrappers.eq(SwPmTicketCardType::getMainName,"单程票");
        queryWrappers.eq(SwPmTicketCardType::getStatus,"1");
        return R.ok(swPmTicketCardTypeService.getBaseMapper().selectList(queryWrappers));
    }

    /**
     * 主类型票卡参数配票
     *
     * @return
     */
    @ApiOperation(value = "主类型票卡参数配票", notes = "主类型票卡参数配票")
    //@SysLog("主类型票卡参数配票")
    @GetMapping("/dict/ticket")
    public R getDictTicket() {
        SwPmTicketCardType swPmTicketCardType = new SwPmTicketCardType();
        LambdaQueryWrapper<SwPmTicketCardType> queryWrappers = Wrappers.query(swPmTicketCardType).lambda();
        queryWrappers.in(SwPmTicketCardType::getMainName,"营销车票","预制单程票","应急纸票");
        queryWrappers.eq(SwPmTicketCardType::getStatus,"1");
        return R.ok(swPmTicketCardTypeService.getBaseMapper().selectList(queryWrappers));
    }

    /**
     * 导入主类型票卡参数
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入主类型票卡参数", notes = "导入主类型票卡参数")
   // @SysLog("导入主类型票卡参数")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwPmTicketCardType.class,
                    new UploadDataListener(swPmTicketCardTypeService, SwPmTicketCardType.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }

    /**
     * 主类型票卡参数字典内部接口
     *
     * @return
     */
    @ApiOperation(value = "主类型票卡参数内部字典", notes = "主类型票卡参数内部字典")
    //@SysLog("主类型票卡参数字典")
    @GetMapping("/dictInner")
    @Inner
    public R getDictInner() {
        return R.ok(swPmTicketCardTypeService.list());
    }
}
